const mysql = require('../../core/mysql');

const log = require('../../core/logger').getLogger("system");
const moment = require('moment');
const common = require('../../core/common');
const stringUtils = require('../../core/util/StringUtils');
const _ = require('lodash');

exports.add = async(ctx) => {
    let landAgent_name = ctx.request.body.landAgent_name ? ctx.request.body.landAgent_name.trim() : '';
    let note = ctx.request.body.note || '';
    if (landAgent_name === '') {
        ctx.body = {
            result: 201,
            message: "请检查参数"
        };
        return;
    }
    let newLandAgent = {
        landAgent_name,
        note,
        proj_numb: 0,
        create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        update_time: moment().format('YYYY-MM-DD HH:mm:ss'),
    };
    try{
        let selectExistlandAgent = await mysql.query(`select * from landagent where landAgent_name = ? `, landAgent_name);
        if (selectExistlandAgent.length > 0 ){
            ctx.body = {
                result: 201,
                message: "已存在此房开商"
            };
            return
        }
        let ret = await mysql.query(`INSERT INTO landagent SET ?`, newLandAgent);
        if (!ret || ret.insertId === -1){
            ctx.body = {
                result: 202,
                message: "新增房开商失败"
            };
        } else {
            ctx.body = {
                result: 200,
                id: ret.insertId,
                message: "新增房开商成功"
            };
        }
    }catch (e) {
        ctx.body = {
            result: 205,
            message: "错误"+e.toString()
        };
    }
}

exports.delete = async(ctx) => {
    let landAgent_id = ctx.params.landAgent_id || 0;
    if (landAgent_id <= 0) {
        ctx.body = {
            result: 201,
            message: "请检查参数"
        };
        return
    }
    try{
        ret = await mysql.query(`delete from landagent WHERE landAgent_id = ?`, landAgent_id);
        if (!ret || ret.affectedRows === 0){
            ctx.body = {
                result: 202,
                message: "对象不存在或已删除"
            };
        } else {
            ctx.body = {
                result: 200,
                message: "删除成功"
            };
        }
    }catch (e) {
        ctx.body = {
            result: 205,
            message: "错误"+e.toString()
        };
    }
}

exports.update = async(ctx) => {
    let landAgent_id = ctx.request.body.landAgent_id || 0;
    let landAgent_name = ctx.request.body.landAgent_name ? ctx.request.body.landAgent_name.trim() : '';
    let note = ctx.request.body.note || '';

    if (landAgent_id <= 0 || landAgent_name === '') {
        ctx.body = {
            result: 201,
            message: "请检查参数"
        };
    }else{
        try {
            let selectRows = await mysql.query(`SELECT * FROM landagent WHERE landAgent_id = ?`, landAgent_id);
            if (selectRows.length > 0) {
                let selectExistlandAgent = await mysql.query(`select * from landagent where landAgent_name = ? and landAgent_id <> ? `, [landAgent_name,landAgent_id]);
                if (selectExistlandAgent.length > 0){
                    ctx.body = {
                        result: 202,
                        message: "已存在此房开商"
                    };
                    return;
                }
                selectRows[0].landAgent_name = landAgent_name;
                selectRows[0].note = note;
                selectRows[0].update_time = moment().format('YYYY-MM-DD HH:mm:ss');
                ret = await mysql.query(`UPDATE landagent SET ? WHERE landAgent_id= ? `, [selectRows[0],landAgent_id]);
                if (!ret || ret.affectedRows === 0) {
                    ctx.body = {
                        result: 202,
                        message: "更新失败"
                    };
                } else {
                    ctx.body = {
                        result: 200,
                        message: "更新成功"
                    };
                }
            } else {
                ctx.body = {
                    result: 203,
                    message: "对象不存在或者已删除"
                };
            }
        }catch (e) {
            ctx.body = {
                result: 205,
                message: "错误"+e.toString()
            };
        }
    }
}


exports.getList = async(ctx) => {
    let create_user_id = ctx.user.id || '';
    let keyword = ctx.query.keyword ? ctx.query.keyword.trim() : '';
    let counts = ctx.query.counts || 10;
    let indexfrom = ctx.query.indexfrom || 0;

    if (create_user_id === ''){
        ctx.body = {
            result: 201,
            message: "无效的用户id"
        };
    } else {
        try {
            let results = (keyword === '') ? await mysql.query(`SELECT * FROM landagent ORDER BY landAgent_id DESC LIMIT ${indexfrom}, ${counts} ` )
                : await mysql.query(`SELECT * FROM landagent WHERE landAgent_name like '%${keyword}%' ORDER BY landAgent_id DESC LIMIT ${indexfrom}, ${counts} ` );
            let totalResult = (keyword === '') ? await mysql.query(`SELECT COUNT(landAgent_id) AS total FROM landagent `)
                : await mysql.query(`SELECT COUNT(landAgent_id) AS total FROM landagent WHERE landAgent_name like '%${keyword}%' `);
            ctx.body = {
                result: 200,
                list:results,
                totalpage:Math.ceil((totalResult.length > 0 ? totalResult[0].total : 0)/counts),
                totalcounts:totalResult.length > 0 ? totalResult[0].total : 0,
                message: "查询成功"
            };
        }catch (e) {
            ctx.body = {
                result: 205,
                message: "错误 " + e.toString()
            };
        }
    }
}